# 各种统计信息

config = require('../config')
Schema = config.Schema
mongoose = config.mongoose

OpStatSchema = new Schema({
  id:
    type: String
    default: "basic"
  op:
    type: Schema.Types.Mixed
    default: {}
})

OpStatSchema.set('autoIndex',false)
OpStatSchema.index {id:1}

OpStatSchema.statics.get = (cb,id="basic") ->
  OpStat.findOne {id}, (err,obj) ->
    if err
      return cb err
    return cb null, obj?.op or {}

OpStatSchema.statics.set = (new_op,cb,id="basic") ->
  OpStat.findOne {id}, (err, obj) ->
    if err
      return cb err
    unless obj
      obj = new OpStat {id}
    for own k,v of new_op
      obj.op[k] = v
    obj.markModified("op")
    obj.save (err,result) ->
      if err
        cb "#{err}:#{JSON.stringify obj}"
      else
        cb null

OpStatSchema.statics.clear = (cb=()->) ->
  OpStat.remove {}, cb

exports.OpStat = OpStat = mongoose.model 'OpStat', OpStatSchema

